package com.moxiao.yfjh.module.blind.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moxiao.yfjh.framework.common.util.http.HttpUtils;
import com.moxiao.yfjh.framework.common.util.json.JsonUtils;
import com.tencentyun.TLSSigAPIv2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/1/23 14:58
 **/


@Component
@Slf4j
public class TencentCloudImUtil {
    private static final String HTTPS_URL_PREFIX = "https://console.tim.qq.com/";
    private static final String APP_MANAGER = "administrator";
    private static final String REDIS_IM_USER_SIG = "silence:im_user_sig::";

    @Value("${silence.tencent.cloud.im.sdkAppId}")
    private long sdkAppId;
    @Value("${silence.tencent.cloud.im.key}")
    private String key;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 获取腾讯云用户签名
     */
    public String getTxCloudUserSig() {
        String userSig = stringRedisTemplate.opsForValue().get(REDIS_IM_USER_SIG + APP_MANAGER);
        if (StringUtils.isEmpty(userSig)){
            TLSSigAPIv2 tlsSigApi = new TLSSigAPIv2(sdkAppId, key);
             userSig = tlsSigApi.genUserSig(APP_MANAGER, 86400);
            stringRedisTemplate.opsForValue().set(REDIS_IM_USER_SIG + APP_MANAGER, userSig, Duration.ofHours(24));
        }
        return userSig;
    }


    /**
     * 获取腾讯im请求路径
     */
    private String getHttpsUrl(String imServiceApi, Integer random) {
        return String.format("%s%s?sdkappid=%s&identifier=%s&usersig=%s&random=%s&contenttype=json",
                HTTPS_URL_PREFIX, imServiceApi, sdkAppId, APP_MANAGER, this.getTxCloudUserSig(), random);
    }

    /**
     * 导入单个账号
     * @param userId 用户id
     */
    public void accountImport(String userId) {
        accountImport(userId, null);
    }
    public void accountImport(String userId, String userName) {
        accountImport(userId, userName, null);
    }

    public void accountImport(String userId, String userName, String faceUrl) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.ACCOUNT_IMPORT, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Identifier", userId);
        if (StringUtils.isNotEmpty(userName)) {
            jsonObject.put("Nick", userName);
        }
        if (StringUtils.isNotEmpty(faceUrl)) {
            jsonObject.put("FaceUrl", faceUrl);
        }

        String s = HttpUtils.sendPost(httpsUrl, JsonUtils.toJsonString(jsonObject));
//        System.out.println(s);
    }


    public void portraitSet(String userId, String userName, String faceUrl) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.PORTRAIT_SET, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("From_Account", userId);
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        if (StringUtils.isNotEmpty(userName)) {
            jsonObject1.put("Tag", "Tag_Profile_IM_Nick");
            jsonObject1.put("Value", userName);
            jsonArray.add(jsonObject1);
        }
        JSONObject jsonObject2 = new JSONObject();
        if (StringUtils.isNotEmpty(faceUrl)) {
            jsonObject2.put("Tag", "Tag_Profile_IM_Image");
            jsonObject2.put("Value", faceUrl);
            jsonArray.add(jsonObject2);
        }
        jsonObject.put("ProfileItem",jsonArray);
        String str = HttpUtils.sendPost(httpsUrl, JsonUtils.toJsonString(jsonObject));
        log.info("：IM修改个人资料：参数{}返回-{}", jsonArray, str);
        System.out.println(str);
    }




}
